﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eniux.Windows.ThreeDEngine.Helper;
using System.Windows;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 屏幕空间线元素
    /// </summary>
    public class LinesVisual3D : ScreenSpaceVisual3D
    {

        #region 变量
        /// <summary>
        ///  几何对象线生成器
        /// </summary>
        private readonly LineGeometryBuilder builder;

        #endregion

        #region 注册属性
        /// <summary>
        ///  线粗细属性
        /// </summary>
        public static readonly DependencyProperty ThicknessProperty = DependencyProperty.Register(
            "Thickness", typeof(double), typeof(LinesVisual3D), new UIPropertyMetadata(1.0, GeometryChanged));

        /// <summary>
        /// 获取或设置线粗细
        /// </summary>
        public double Thickness
        {
            get{return (double)GetValue(ThicknessProperty);}
            set{SetValue(ThicknessProperty, value);}
        }
        #endregion

        #region 构造、析构
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public LinesVisual3D()
        {
            this.builder = new LineGeometryBuilder(this);
        }

        #endregion


        #region 方法

        /// <summary>
        /// 更新线对象
        /// </summary>
        protected override void UpdateGeometry()
        {
            this.mesh.Positions = null;
            if (this.Points != null)
            {
                int n = this.Points.Count;
                if (this.mesh.TriangleIndices.Count != n * 3)
                {
                    this.mesh.TriangleIndices = this.builder.CreateIndices(n);
                }

                this.mesh.Positions = this.builder.CreatePositions(this.Points, this.Thickness, this.DepthOffset);
            }
        }

        /// <summary>
        /// 更新空间转换信息
        /// </summary>
        /// <returns></returns>
        protected override bool UpdateTransforms()
        {
            return this.builder.UpdateTransforms();
        }

        #endregion
    }
}
